// pages/card/mycard/mycard.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: getApp().globalData.CustomBar,
    scrollHeight: '100vh',
    cardList: [],
  },
  qryCardList() {
    Toast.loading({
      message: '加载中...',
      forbidClick: true,
    });
    getApp().callContainer('/api/user/card/list', {}, (res) => {
      this.setData({ cardList: res.data.data.cardList || [] });
      this.loadTmpFile();
      this.getScrollHeight();
      Toast.clear();
    }, () => {
      Toast.clear();
    })
  },
  // 缓存图片
  loadTmpFile() {
    for (const card of this.data.cardList) {
      if (!card.cardImgUrl) {
        continue;
      }
      getApp().getTempFilePath(card.cardImgUrl).then((res) => {
        card.tmpCardImgUrl = res;
        this.setData({ cardList: this.data.cardList });
      })
    }
  },
  // 跳转到申请卡片
  onClickToApplyCard() {
    wx.navigateTo({
      url: '/pages/user/mycard/apply/index/index',
    })
  },
  // 跳转到卡片详情
  onClickCardDetl(event) {
    const item = event.currentTarget.dataset.operation;
    wx.navigateTo({
      url: "/pages/user/mycard/detail/detail?cardId=" + item.cardId + "&cardName=" + item.cardName+ "&cardCount=" + item.count,
    })
  },
  getScrollHeight() {
    // 获取内容区域的高度
    wx.createSelectorQuery().selectViewport().scrollOffset((res) => {
      this.setData({ scrollHeight: res.scrollHeight + 'px' });
    }).exec()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.qryCardList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.qryCardList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.qryCardList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})